package cn.quang.utils.lang;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class StringUtils {

    /**
     * 输入一个字符串，并以大写字母的方式返回第一个字符
     * 必须输入英文字符串
     */
    public static char firstCharToUpperCase(String original) {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        return Character.toUpperCase(original.charAt(0));
    }

    /**
     * 输入一个字符串，并以小写字母的方式返回第一个字符
     * 必须输入英文字符串
     */
    public static char firstCharToLowerCase(String original) {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        return Character.toLowerCase(original.charAt(0));
    }

    /**
     * 输入一个字符串，并以大写字母的方式返回最后一个字符
     * 必须输入英文字符串
     */
    public static char lastCharToUpperCase(String original) {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        return Character.toUpperCase(original.charAt(original.length() - 1));
    }

    /**
     * 输入一个字符串，并以小写字母的方式返回最后一个字符
     * 必须输入英文字符串
     */
    public static char lastCharToLowerCase(String original) {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        return Character.toLowerCase(original.charAt(original.length() - 1));
    }

    /**
     * 将原始字符串通过指定分隔符切割后放入Set集合中
     */
    public static Set<String> splitToSet(String original, String separator) {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        if(separator == null || separator.isEmpty()) {
            throw new IllegalArgumentException("separator is null or empty");
        }
        String[] split = original.split(separator);
        return new HashSet<>(Arrays.asList(split));
    }

    /**
     * 将原始字符串通过指定分隔符切割后放入List集合中
     */
    public static List<String> splitToList(String original, String separator) {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        if(separator == null || separator.isEmpty()) {
            throw new IllegalArgumentException("separator is null or empty");
        }
        String[] split = original.split(separator);
        return Arrays.asList(split);
    }

    /**
     * 如果输入的字符串长度超过指定长度，则截取指定长度的字符串
     */
    public static String subStringLength(String original, int maxLength) {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        if(maxLength < 0) {
            throw new IllegalArgumentException("length is less than 0");
        }
        if(original.length() <= maxLength) {
            return original;
        }
        return original.substring(0, maxLength);
    }

    /**
     * 如果输入的字符串字节数超过指定字节，则截取指定字节的字符长度
     */
    public static String subStringBytes(String original, int maxBytes, String charset) throws UnsupportedEncodingException {
        if(original == null || original.isEmpty()) {
            throw new IllegalArgumentException("original is null or empty");
        }
        String trimmedStr = original.trim();
        int strLength = trimmedStr.length();
        if(strLength == 0 || trimmedStr.getBytes(charset).length <= maxBytes) {
            return trimmedStr;
        }

        // 计算并截取
        int totalCount = 0;
        for(int i = 0; i < strLength; i++) {
            char ch = trimmedStr.charAt(i);
            int byteLen = String.valueOf(ch).getBytes(charset).length;
            totalCount += byteLen;

            if(totalCount > maxBytes) {
                return trimmedStr.substring(0, i);
            }
        }

        return trimmedStr;
    }



    /**
     * 对标String.startsWith，忽略大小写
     *
     * @param original 原始字符
     * @param prefix 前缀
     * @return true or false
     */
    public static boolean startsWithIgnoreCase(String original, String prefix) {
        return startsWithIgnoreCase(original, prefix, 0);
    }

    /**
     * 对标String.startsWith，忽略大小写
     *
     * @param original 原始字符
     * @param prefix 前缀
     * @param offset 偏移量
     * @return true or false
     */
    public static boolean startsWithIgnoreCase(String original, String prefix, int offset) {
        char[] ta = original.toCharArray();
        char[] pa = prefix.toCharArray();
        int pc = pa.length;
        int to = offset;
        int po = 0;

        // Note: offset might be near -1>>>1.
        if ((offset < 0) || (offset > ta.length - pc)) {
            return false;
        }
        while (--pc >= 0) {
            char thisOriginalChar = ta[to++];
            char thisPrefixChar = pa[po++];
            char thisPrefixCharAnotherCase = Character.isLowerCase(thisPrefixChar) ?
                    Character.toUpperCase(thisPrefixChar) : Character.toLowerCase(thisPrefixChar);

            if (thisOriginalChar != thisPrefixChar && thisOriginalChar != thisPrefixCharAnotherCase) {
                return false;
            }
        }
        return true;
    }
}
